import { Component, OnInit } from '@angular/core';
import { ToastController } from '@ionic/angular';
import { ApiUrl } from '../api/ApiUrl';
import { ApiService } from '../api/api.service';
import { AnswerVO } from '../vo/Answervo';
import { Router } from '@angular/router';
@Component({
  selector: 'app-advice',
  templateUrl: './advice.page.html',
  styleUrls: ['./advice.page.scss'],
})
export class AdvicePage implements OnInit {
  ctitle: '';
  ccontent: '';
  item: AnswerVO;
  constructor(private toastController: ToastController, public api: ApiService, private router: Router) { }

  ngOnInit() {
  }
  submit() {
    if (undefined === this.ctitle || this.ctitle.length === 0) {
      this.presentToast('请输入标题');
      return;
    }
    if (undefined === this.ccontent || this.ccontent.length === 0) {
      this.presentToast('请输入内容');
      return;
    }
    let iuserid: Number = 0;
    if (null != ApiUrl.user) {
      // 赋值用户id
      iuserid = Number(ApiUrl.user.iid);
    }
    this.toSaveAdvice(this.ctitle, this.ccontent, iuserid);


  }

  /**
   * 提交建议
   * @param ctitle 标题
   * @param ccontent 内容
   * @param iuserid 用户id
   */
  toSaveAdvice(ctitle, ccontent, iuserid) {
    this.api.getItem<AnswerVO>(ApiUrl.toSaveAdvice, { 'ctitle': ctitle, 'ccontent': ccontent, 'icreator': iuserid }).subscribe(response => {
      this.item = response;
      if (this.item.success) {
        this.presentToast(this.item.msg);
        this.router.navigate(['/tabs']);
      } else {
        this.presentToast(this.item.msg);
      }
    }, () => {
      console.log('出错了');
    });

  }


  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }
}
